using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;


namespace Zephyris_Space_Mercs
{
    class MainMenu
    {
        int timeCounter = 0;

        List<ClickableRegion> clickableRegions = new List<ClickableRegion>();

        UI interfaceUI;
        SpriteFont uiFont;
        SpriteFont uiFont2;
        Texture2D uiBackground;
        Texture2D uiButton;

        GraphicsDevice device;

        public void PlayMusic(Song mainMusic)
        {
            MediaPlayer.Play(mainMusic);
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Volume = 1.0f;
        }

        public void Init(SpriteBatch spriteBatch, SpriteBatch uiBatch, GraphicsDevice device)
        {
            interfaceUI = new UI(spriteBatch, uiBatch);
            this.device = device;
        }

        public void LoadContent(Texture2D uiBackground, Texture2D uiButton, SpriteFont uiFont, SpriteFont uiFont2)
        {
            this.uiBackground = uiBackground;
            this.uiButton = uiButton;
            this.uiFont = uiFont;
            this.uiFont2 = uiFont2;
        }
        
        public void DrawScreen()
        {
            timeCounter++;
            if (timeCounter > Constants.timeCounterVal)
                timeCounter = 0;

            interfaceUI.drawScreen(uiBackground);
            clickableRegions.Clear();

            Vector2 topleft = new Vector2(0, 0);
            Vector2 bottomright = new Vector2(0, 0);
            
            topleft = new Vector2(20, (device.Viewport.Height / 2) - uiButton.Height);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "Singleplayer");
            AddClickable(topleft, bottomright);

            topleft = new Vector2(device.Viewport.Width - uiButton.Width - 20, topleft.Y);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "Multiplayer");
            AddClickable(topleft, bottomright);

            topleft = new Vector2(20, (device.Viewport.Height / 2) - uiButton.Height + 120);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "   Settings");
            AddClickable(topleft, bottomright);

            topleft = new Vector2(device.Viewport.Width - uiButton.Width - 20, topleft.Y);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "     About");
            AddClickable(topleft, bottomright);

            topleft = new Vector2((device.Viewport.Width / 2) - (uiButton.Width / 2), (device.Viewport.Height / 2) - uiButton.Height + 60);
            bottomright = new Vector2(topleft.X + this.uiButton.Width, topleft.Y + this.uiButton.Height);
            interfaceUI.drawButton(uiButton, topleft, uiFont, "    Hangar");
            AddClickable(topleft, bottomright);
        }

        public void AddClickable(Vector2 topleftCorner, Vector2 bottomrightCorner)
        {
            ClickableRegion clickableRegion = new ClickableRegion();
            clickableRegion.TopLeftCorner = topleftCorner;
            clickableRegion.BottomRightCorner = bottomrightCorner;
            clickableRegions.Add(clickableRegion);
        }

        public UIPages.UIPage CheckClick(Vector2 clickedRegion)
        {
            if (timeCounter > Constants.timeCounterVal / 2)
            {
                for (int i = 0; i < clickableRegions.Count; i++)
                {
                    switch (i)
                    {
                        case 0:
                            if ((clickedRegion.X > clickableRegions[0].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[0].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[0].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[0].BottomRightCorner.Y))
                            {
                                timeCounter = 0;
                                return UIPages.UIPage.SinglePlayerMenu;
                            }
                            break;

                        case 1:
                            if ((clickedRegion.X > clickableRegions[1].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[1].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[1].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[1].BottomRightCorner.Y))
                            {
                                timeCounter = 0;
                                return UIPages.UIPage.MultiPlayerMenu;
                            }
                            break;

                        case 2:
                            if ((clickedRegion.X > clickableRegions[2].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[2].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[2].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[2].BottomRightCorner.Y))
                            {
                                timeCounter = 0;
                                return UIPages.UIPage.SettingsMenu;
                            }
                            break;

                        case 3:
                            if ((clickedRegion.X > clickableRegions[3].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[3].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[3].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[3].BottomRightCorner.Y))
                            {
                                timeCounter = 0;
                                return UIPages.UIPage.AboutMenu;
                            }
                            break;

                        case 4:
                            if ((clickedRegion.X > clickableRegions[4].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[4].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[4].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[4].BottomRightCorner.Y))
                            {
                                timeCounter = 0;
                                return UIPages.UIPage.HangarMenu;
                            }
                            break;
                    }
                }
            }
            return 0;
        }
    }
}
